package com.app.scale.bean;


import android.text.TextUtils;

import com.alibaba.fastjson.annotation.JSONField;
import com.app.scale.basis.Constants;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;

import my.FileUtils;
import my.LogUtil;

public class ContactListBean extends BaseListBean {

    private ArrayList<ContactListData> list = new ArrayList<ContactListData>();

    private ArrayList<ContactListData> listResult = new ArrayList<>();

    public ArrayList<ContactListData> getList() {
        return list;
    }

    public void setList(ArrayList<ContactListData> list) {
        this.list = list;
    }

    @JSONField(serialize = false)
    public void processList(){
       listResult= new ArrayList<>();
       int level = 0;
        for(ContactListData userZoneListData:list){
//            userZoneListData.setLevel(level);
            addChileDataInList(userZoneListData,listResult,level);
        }
        LogUtil.i("getItemByListPosition---size=--->"+listResult.size());
    }
    @JSONField(serialize = false)
    private void addChileDataInList(ContactListData data,ArrayList<ContactListData> list,int currentLevel){
        data.setLevel(currentLevel);
        list.add(data);
        if(!data.isLeaf_BOOLEAN() && data.getChildren().size() > 0 && data.isExpand()){
            for(ContactListData userZoneListData:data.getChildren()){
                addChileDataInList(userZoneListData,list,currentLevel+1);
            }
        }
    }

    @JSONField(serialize = false)
    public int getListResultCount(){
        return listResult == null?0:listResult.size();
    }

    @JSONField(serialize = false)
    public ContactListData getItemByListPosition(int position){
        return listResult.get(position);
    }

    @JSONField(serialize = false)
    public void expandOrCloseByListPosition(int position){
        ContactListData  listData =  getItemByListPosition(position);
//        LogUtil.i("expandOrCloseByListPosition--->"+position);
//        LogUtil.i("expandOrCloseByListPosition--->"+listData.getTitle());
//        LogUtil.i("expandOrCloseByListPosition--->"+listData.getChildren().size());
//        LogUtil.i("expandOrCloseByListPosition--level->"+ listData.getLevel());

        for(ContactListData data:list){
            changeExpandById(data,listData.getId());
        }
        processList();
    }
    @JSONField(serialize = false)
    private boolean changeExpandById(ContactListData data,String id){
        if(data.getId().equals(id)){
            data.setExpand(!data.isExpand());
            return true;
        }
        if(!data.isLeaf_BOOLEAN() && data.getChildren().size() > 0 ){
            for(ContactListData userZoneListData:data.getChildren()){
                if(changeExpandById(userZoneListData,id))return true;
            }
        }
        return false;
    }

    @JSONField(serialize = false)
    public BaseListDataListBean getDeptSearchList(){
        BaseListDataListBean listBean = new BaseListDataListBean();
        listBean.getList().add(new BaseListData("","全部"));
        for(ContactListData data:list){
            addDeptData(data,listBean);
        }
        return listBean;
    }
    @JSONField(serialize = false)
    private void addDeptData(ContactListData data,BaseListDataListBean listBean){
        if(listBean.getDataById(data.getId()) != null)return;
        BaseListData listData = new BaseListData(data.getId(),data.getTitle());
        listBean.getList().add(listData);
    }
    @JSONField(serialize = false)
    public ContactListBean getByDept(BaseListData baseListData){
        ContactListBean listBean = new ContactListBean();
        if(baseListData == null || TextUtils.isEmpty(baseListData.getId())){
            try {
                //深浅复制问题， 之前用浅复制， 后续对此对象的修改都会影响到复制的对象。必须使用深复制
                listBean.getList().addAll(FileUtils.deepCopy(getList()));
            } catch (IOException e) {
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
        }else {
            for(ContactListData data:list){
                if(data.getId().equals(baseListData.getId())){
//                    data.setExpand(true);//搜索出来的展开
                    listBean.getList().add(data);
                }
            }
        }
        LogUtil.i("getByDept-->"+listBean.getList().size());
        return listBean;
    }
    @JSONField(serialize = false)
    public ContactListBean getBySearchKey(String key){
        ContactListBean listBean = new ContactListBean();
        if(TextUtils.isEmpty(key)){
            try {
                //深浅复制问题， 之前用浅复制， 后续对此对象的修改都会影响到复制的对象。必须使用深复制
                listBean.getList().addAll(FileUtils.deepCopy(getList()));
            } catch (IOException e) {
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
        }else {
            for(ContactListData data:list){
                addItemBySearchKey(data,listBean,key);
            }
        }
        LogUtil.i("getByDept-->"+listBean.getList().size());
        return listBean;
    }
    @JSONField(serialize = false)
    private void addItemBySearchKey(ContactListData data,ContactListBean listBean ,String key){
        ContactBizData bizData;
        if(data.getTitle().contains(key)){
            listBean.getList().add(data);
            return ;
        }else if(data.isLeaf_BOOLEAN() && data.getBizData() != null){
            bizData = data.getBizData();
            if(bizData.getName().contains(key) || bizData.getDeptName().contains(key) || bizData.getPhone().contains(key) || bizData.getPost().contains(key)){
                listBean.getList().add(data);
                return ;
            }
        }
        if(!data.isLeaf_BOOLEAN() && data.getChildren().size() > 0 ){
            for(ContactListData userZoneListData:data.getChildren()){
                addItemBySearchKey(userZoneListData,listBean,key);
            }
        }
    }


    public static final String FILE_CACHE = Constants.DIR_FILECACHE
            + "ff_firebrige_contact_list.data";

    @JSONField(serialize = false)
    public static ContactListBean getFromCache() {
        ContactListBean data = (ContactListBean) FileUtils.readObject(FILE_CACHE);
        if (data == null) {
//            data = new ContactListBean();
        }
        return data;
    }

    @JSONField(serialize = false)
    public static boolean isCacheExist(){
        return FileUtils.isFileExist(FILE_CACHE);
    }

    @JSONField(serialize = false)
    public static long getLastCacheTime(){
        return FileUtils.getLasModified(FILE_CACHE);
    }


    @JSONField(serialize = false)
    public void save() {
        FileUtils.cacheObject(FILE_CACHE, this);
    }
    @JSONField(serialize = false)
    public static void delete() {
        FileUtils.cacheObject(FILE_CACHE, null);
    }

    public void addOrRemoveData(ContactListData data){
        Iterator<ContactListData> iterator = list.iterator();
        while (iterator.hasNext()){
            ContactListData  itemData = iterator.next();
            if(data.getId().equals(itemData.getId())){
                iterator.remove();
                return;
            }
        }
        list.add(data);
    }

    public boolean isDateChecked(ContactListData data){
        for (ContactListData itemData:list){
            if(data.getId().equals(itemData.getId())){
                return true;
            }
        }
        return false;
    }

    public boolean isDateGroupAllChecked(ContactListData data){
//        if(data.getChildren() == null || data.getChildren().size() == 0)return true;
        for (ContactListData itemData:data.getChildren()){
            if(itemData.isLeaf_BOOLEAN()){
                if(!isDateChecked(itemData))return false;
            }else {
                if(!isDateGroupAllChecked(itemData))return false;
            }
        }
        return true;
    }

    public boolean isDateGroupHasChecked(ContactListData data){
//        if(data.getChildren() == null || data.getChildren().size() == 0)return true;
        for (ContactListData itemData:data.getChildren()){
            if(itemData.isLeaf_BOOLEAN()){
                if(isDateChecked(itemData))return true;
            }else {
                if(isDateGroupHasChecked(itemData))return true;
            }
        }
        return false;
    }

    public boolean isDateGroupHasCheckedByDept(ContactListData data){
//        if(data.getChildren() == null || data.getChildren().size() == 0)return true;
        for (ContactListData itemData:data.getChildren()){
                if(isDateChecked(itemData)){
                    return true;
                }
                if(isDateGroupHasCheckedByDept(itemData))return true;
        }
        return false;
    }

    public static boolean isGroupDateNoMember(ContactListData data){
        if(data.getChildren() == null || data.getChildren().size() == 0)return true;
        for (ContactListData itemData:data.getChildren()){
            if(itemData.isLeaf_BOOLEAN()){
               return  false;
            }else {
                if(!isGroupDateNoMember(itemData))return false;
            }
        }
        return true;
    }
    public boolean isAllCheck(ContactListBean contactListBean){
        for(ContactListData itemData:contactListBean.getList()){
            if(itemData.isLeaf_BOOLEAN()){
                if(!isDateChecked(itemData))return false;
            }else {
                if(!isDateGroupAllChecked(itemData))return false;
            }
        }
        return true;
    }
    public boolean setAllCheck(ContactListBean contactListBean){
        for(ContactListData itemData:contactListBean.getList()){
            if(itemData.isLeaf_BOOLEAN()){
                if(!isDateChecked(itemData)){
                    addOrRemoveData(itemData);
                }
            }else {
                setGroupAllCheck(itemData);
            }
        }
        return true;
    }
    public void setGroupAllCheck(ContactListData groupData){
        for (ContactListData itemData:groupData.getChildren()){
            if(!itemData.isLeaf_BOOLEAN()){
                setGroupAllCheck(itemData);
            }else {
                if(!isDateChecked(itemData)){
                    addOrRemoveData(itemData);
                }
            }
        }
    }

    public void setGroupAllRemove(ContactListData groupData){
        for (ContactListData itemData:groupData.getChildren()){
            if(!itemData.isLeaf_BOOLEAN()){
                setGroupAllRemove(itemData);
            }else {
                if(isDateChecked(itemData)){
                    addOrRemoveData(itemData);
                }
            }
        }
    }

    public String[] getParamsString(){
        if(list.size() == 0)return null;
        String[] strs = new String[list.size()];
        for (int i = 0; i < strs.length; i ++){
            strs[i] = list.get(i).getId();
        }
        return strs;
    }
}
